package com.lemon.smartsafetynew.ui.activity.hidden;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.InputFilter;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.bigkoo.pickerview.OptionsPickerView;
import com.lemon.android.http.LemonRequestParams;
import com.lemon.safecloud.greendao.HiddenLevel;
import com.lemon.safecloud.greendao.HiddenType;
import com.lemon.safecloud.greendao.SelfCheckType;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.entity.hidden.HiddenTypePickViewVo;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.http.service.HiddenServices;
import com.lemon.smartsafetynew.ui.activity.base.PhotoSelectBaseActivity;
import com.lemon.smartsafety.ui.adapter.common.SelectEvidenceAdapter;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.smartsafetynew.entity.User;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.contact.DepartmentVo;
import com.lemon.smartsafetynew.entity.contact.PersonVo;
import com.lemon.smartsafetynew.entity.event.common.ViewEvent;
import com.lemon.smartsafetynew.entity.event.contact.DepartmentEvent;
import com.lemon.smartsafetynew.entity.event.contact.PersonEvent;
import com.lemon.smartsafetynew.entity.hidden.SafeHiddenVo;
import com.lemon.smartsafetynew.net.services.SafeHiddenServices;
import com.lemon.smartsafetynew.ui.activity.contact.DepartmentListActivity;
import com.lemon.smartsafetynew.ui.activity.contact.PersonListActivity;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.common.PointLengthFilter;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.constants.SafeCloudConstants;
import com.lemon.smartsafetynew.util.constants.TimeFormatConstants;
import com.lemon.smartsafetynew.util.user.AccountUtil;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.wdullaer.materialdatetimepicker.date.DatePickerDialog;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.Bind;

/**
 * Created by Andrew on 2017/9/8.
 */

public class SafeHiddenCreateHiddenActivity extends PhotoSelectBaseActivity {

    @Bind(R.id.hidden_submit_btn)
    Button hiddenSubmitBtn;
    @Bind(R.id.hidden_check_position_tv)
    TextView hiddenCheckPositionTv;
    @Bind(R.id.hidden_check_position_et)
    EditText hiddenCheckPositionEt;
    @Bind(R.id.hidden_check_type_tv)
    TextView hiddenCheckTypeTv;
    @Bind(R.id.hidden_check_type_arrow_imageView)
    ImageView hiddenCheckTypeArrowImageView;
    @Bind(R.id.hidden_check_type_layout)
    LinearLayout hiddenCheckTypeLayout;
    @Bind(R.id.hidden_check_time_tv)
    TextView hiddenCheckTimeTv;
    @Bind(R.id.hidden_check_time_arrow_imageView)
    ImageView hiddenCheckTimeArrowImageView;
    @Bind(R.id.hidden_check_time_layout)
    LinearLayout hiddenCheckTimeLayout;
    @Bind(R.id.hidden_check_person_tv)
    TextView hiddenCheckPersonTv;
    @Bind(R.id.hidden_check_person_arrow_imageView)
    ImageView hiddenCheckPersonArrowImageView;
    @Bind(R.id.hidden_check_person_layout)
    LinearLayout hiddenCheckPersonLayout;
    @Bind(R.id.hidden_type_tv)
    TextView hiddenTypeTv;
    @Bind(R.id.hidden_category_arrow_imageView)
    ImageView hiddenCategoryArrowImageView;
    @Bind(R.id.hidden_type_layout)
    LinearLayout hiddenTypeLayout;
    @Bind(R.id.hidden_level_tv)
    TextView hiddenLevelTv;
    @Bind(R.id.hidden_level_arrow_imageView)
    ImageView hiddenLevelArrowImageView;
    @Bind(R.id.hidden_level_layout)
    LinearLayout hiddenLevelLayout;
    @Bind(R.id.hidden_remark_tv)
    TextView hiddenRemarkTv;
    @Bind(R.id.hidden_remark_et)
    MaterialEditText hiddenRemarkEt;
    @Bind(R.id.hidden_remark_layout)
    LinearLayout hiddenRemarkLayout;
    @Bind(R.id.attachment_tv)
    TextView attachmentTv;
    @Bind(R.id.mSelectRecycleView)
    RecyclerView mSelectRecycleView;
    @Bind(R.id.mLoadRecycleView)
    RecyclerView mLoadRecycleView;
    @Bind(R.id.department_tv)
    TextView departmentTv;
    @Bind(R.id.department_arrow_imageView)
    ImageView departmentArrowImageView;
    @Bind(R.id.department_layout)
    LinearLayout departmentLayout;
    @Bind(R.id.responsible_tv)
    TextView responsibleTv;
    @Bind(R.id.responsible_arrow_imageView)
    ImageView responsibleArrowImageView;
    @Bind(R.id.responsible_layout)
    LinearLayout responsibleLayout;
    @Bind(R.id.expect_finish_date_tv)
    TextView expectFinishDateTv;
    @Bind(R.id.expect_finish_date_arrow_imageView)
    ImageView expectFinishDateArrowImageView;
    @Bind(R.id.expect_finish_date_layout)
    LinearLayout expectFinishDateLayout;
    @Bind(R.id.expect_money_et)
    EditText expectMoneyEt;

    //@Bind(R.id.expect_money_tv)
    //TextView expectMoneyTv;

    @Bind(R.id.hidden_scrollview)
    ScrollView hiddenScrollview;
    @Bind(R.id.hidden_layout)
    RelativeLayout hiddenLayout;

    // ============ 参数 ==============
    private User user;

    /**
     * Dialog 的类型
     */
    private enum DialogType {
        TYPE_CATE, TYPE_LEVEL;
    }

    /**
     * 类型
     */
    private String[] aSelfTypeName;
    private int[] aSelfTypeId;
    private int typeWhich = -1;
    /**
     * 级别
     */
    private String[] aHiddenLevelName;
    private int[] aHiddenLevelId;
    private int levelWhich = -1;


    private MaterialDialog.Builder mMaterialDialog;

    /**
     * 隐患分类选择
     */
    private OptionsPickerView<HiddenType> mHiddenCategoryView;
    private HiddenTypePickViewVo mPickViewVo;
    private List<HiddenType> mHiddenTypes;
    private int mHiddenTypePosition1 = -1, mHiddenTypePosition2 = -1;

    /**
     * 部门
     */
    private DepartmentVo departmentVo;

    /**
     * 人员
     */
    private PersonVo personVo;


    @Override
    protected void initTitle() {
        this.setBarTitle("新建隐患");
    }

    @Override
    protected void callFinish() {
        AppService.getBus().post(new ViewEvent(SafeHiddenCreateHiddenActivity.class.getName())); //TODO 可以再优化下
        AppManager.getAppManager().finishActivity(SafeHiddenCreateHiddenActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_safe_hidden_create_hidden;
    }

    @Override
    protected void initViews() {
        super.initViews();

        initData();
        initUi();
    }

    /**
     * 初始化数据变量
     */
    private void initData() {
        user = AccountUtil.getUser();

        if (user == null) {
            mHUDUtil.showErrorTip("用户信息为空，请重新登陆！", new HUDUtil.CallBack() {
                @Override
                public void onCall() {
                    callFinish();
                }
            });
        }
    }

    /**
     * 初始化页面
     */
    private void initUi() {

        hiddenCheckPositionTv.setVisibility(View.GONE);
        hiddenCheckPositionEt.setVisibility(View.VISIBLE);
        hiddenCheckPersonArrowImageView.setVisibility(View.GONE);

        hiddenCheckPersonTv.setText(CommonUtil.avoidNullString(user.getUserName()));
        hiddenCheckTimeTv.setText(CommonUtil.secondToTimeStr(System.currentTimeMillis(), DateTypeEnum.DAY));

        expectMoneyEt.setFilters(new InputFilter[]{new PointLengthFilter()}); //设置输入金额最小单位为分

        hiddenSubmitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (verifyParam()) {
                    postSafeHiddenToCloud();
                }
            }
        });

        hiddenCheckTypeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (aSelfTypeName == null || aSelfTypeId == null) {
                    HiddenServices.init().getHiddenSelfCheckType();
                } else {
                    showSelfCheckTypeDialog();
                }
            }
        });

        hiddenCheckTimeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pickCheckDate();
            }
        });

        hiddenTypeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getHiddenCategoryData();
            }
        });

        hiddenLevelLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (aHiddenLevelId == null || aHiddenLevelName == null) {
                    HiddenServices.init().getHiddenLevel();
                } else {
                    showHiddenLevelDialog();
                }
            }
        });

        departmentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDepartmentList();
            }
        });

        responsibleLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (departmentVo != null) {
                    showPersonList();
                } else {
                    mHUDUtil.showErrorTip("请先选择责任部门！");
                }
            }
        });

        expectFinishDateLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pickFinishDate();
            }
        });
    }

    /**
     * 提交隐患查报时，验证参数是否完整
     */
    private boolean verifyParam() {

        /**
         * 检查部位
         */
        if (StringUtils.isEmpty(hiddenCheckPositionEt.getText().toString().trim())) {
            mHUDUtil.showErrorTip("请填写检查部位。");
            return false;
        }

        /**
         * 检查类型
         */
        if (StringUtils.isEmpty(hiddenCheckTypeTv.getText().toString().trim())) {
            mHUDUtil.showErrorTip("请选择检查类型。");
            return false;
        }

        /**
         * 检查时间
         */
        if (StringUtils.isEmpty(hiddenCheckTimeTv.getText().toString().trim())) {
            mHUDUtil.showErrorTip("请选择检查时间。");
            return false;
        }

        /**
         * 检查人
         */
        if (StringUtils.isEmpty(hiddenCheckPersonTv.getText().toString().trim())) {
            mHUDUtil.showErrorTip("请选择检查人。");
            return false;
        }

        /**
         * 隐患分类
         */
        if (StringUtils.isEmpty(hiddenTypeTv.getText().toString().trim())) {
            mHUDUtil.showErrorTip("请选择隐患分类。");
            return false;
        }

        /**
         * 隐患级别
         */
        if (StringUtils.isEmpty(hiddenLevelTv.getText().toString().trim())) {
            mHUDUtil.showErrorTip("请选择隐患级别。");
            return false;
        }

        /**
         * 隐患阐述
         */
        if (StringUtils.isEmpty(hiddenRemarkEt.getText().toString().trim())) {
            mHUDUtil.showErrorTip("请填写隐患阐述。");
            return false;
        }

        /**
         * 责任部门
         */
        if (StringUtils.isEmpty(departmentTv.getText().toString().trim())) {
            mHUDUtil.showErrorTip("请选择责任部门。");
            return false;
        }

        /**
         * 责任人
         */
        if (StringUtils.isEmpty(responsibleTv.getText().toString().trim())) {
            mHUDUtil.showErrorTip("请选择责任人。");
            return false;
        }

        /**
         * 预计完成时间
         */
        if (StringUtils.isEmpty(expectFinishDateTv.getText().toString().trim())) {
            mHUDUtil.showErrorTip("请选择预计完成时间。");
            return false;
        }

        /**
         * 预计整改金额
         */
        if (StringUtils.isEmpty(expectMoneyEt.getText().toString().trim())) {

            mHUDUtil.showErrorTip("请填写预计整改金额。");
            return false;

        } else {

            String str = expectMoneyEt.getText().toString().trim();

            Pattern r = Pattern.compile(SafeCloudConstants.MONEY_INPUT_REGULAR_EXPRESSION);
            Matcher m = r.matcher(str);

            if(m.matches()) {
                //return true;
            } else {
                mHUDUtil.showErrorTip("输入的预计整改金额超过最大值。");
                return false;
            }

        }

        return true;
    }

    /**
     * 显示自查类型Dialog
     */
    private void showSelfCheckTypeDialog() {
        showSingleDialog(R.string.hidden_prop_dialog_self_check_type, aSelfTypeName, aSelfTypeId, DialogType.TYPE_CATE, hiddenCheckTypeTv, typeWhich);
    }

    /**
     * 显示隐患级别Dialog
     */
    private void showHiddenLevelDialog() {
        showSingleDialog(R.string.hidden_prop_dialog_hidden_level, aHiddenLevelName, aHiddenLevelId, DialogType.TYPE_LEVEL, hiddenLevelTv, levelWhich);
    }

    /**
     * 选择隐患分类选择器
     */
    private void getHiddenCategoryData() {
        if (mHiddenTypes != null && mHiddenTypes.size() != 0) {//节约网络请求的次数，这里如果请求过一次了就不到网络去取了
            showHiddenCategoryPickView(mHiddenTypes);
        } else {
            HiddenServices.init().getHiddenCategory();
        }
    }

    /**
     * 窗口提示（单选）
     *
     * @param title       窗口标题
     * @param items       问题数据
     * @param ints        ID数据
     * @param dialogType  窗口类型
     * @param textView    接受结果的View
     * @param oldPosition 历史位置
     */
    public void showSingleDialog(int title, @NonNull final String[] items, @NonNull final int[] ints, final DialogType dialogType, final TextView textView, int oldPosition) {
        if (oldPosition >= items.length || oldPosition == -1) {
            oldPosition = 0;
        }
        new MaterialDialog.Builder(this).title(title).items(items).itemsCallbackSingleChoice(oldPosition, new MaterialDialog.ListCallbackSingleChoice() {
            @Override
            public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                if (DialogType.TYPE_CATE == dialogType) {//自查类型
                    typeWhich = which;
                } else if (DialogType.TYPE_LEVEL == dialogType) {//隐患分类
                    levelWhich = which;
                }
                if (which != -1) {
                    textView.setText(items[which]);

                    textView.setTag(ints[which]);
                }
                /**
                 * If you use alwaysCallSingleChoiceCallback(), which is discussed below,
                 * returning false here won't allow the newly selected radio button to actually be selected.
                 **/
                return true;
            }
        }).positiveText(R.string.sure_str).show();
    }

    /**
     * 选择器
     *
     * @param pickViewVo
     * @return
     */
    private OptionsPickerView getHiddenTypeOptionPickView(HiddenTypePickViewVo pickViewVo) {
        if (mHiddenCategoryView == null) {
            //选项选择器
            mHiddenCategoryView = new OptionsPickerView<HiddenType>(this);
            mHiddenCategoryView.setOnoptionsSelectListener(mOnOptionsSelectListener);
            mHiddenCategoryView.setPicker(pickViewVo.getOneLevel(), pickViewVo.getTwoLevel(), true);
            mHiddenCategoryView.setTitle(getString(R.string.hidden_prop_dialog_hidden_category));
            mHiddenCategoryView.setCyclic(false, true, true);
            //设置默认选中的三级项目
            //监听确定选择按钮
            if (mHiddenTypePosition1 >= pickViewVo.getOneLevel().size()) {
                mHiddenTypePosition1 = 0;
            }

            if (mHiddenTypePosition2 >= pickViewVo.getTwoLevel().size()) {
                mHiddenTypePosition2 = 0;
            }
            mHiddenCategoryView.setSelectOptions(mHiddenTypePosition1 == -1 ? 0 : mHiddenTypePosition1, mHiddenTypePosition2 == -1 ? 0 : mHiddenTypePosition2);
        }
        return mHiddenCategoryView;

    }

    /**
     * 显示隐患分类选择器
     *
     * @param items
     */
    private void showHiddenCategoryPickView(List<HiddenType> items) {
        mPickViewVo = HiddenServices.getHiddenTypePickViewVo(items);
        if (mPickViewVo != null && mPickViewVo.getOneLevel() != null && mPickViewVo.getTwoLevel() != null) {
            if (mPickViewVo.getOneLevel().size() == 0 || mPickViewVo.getTwoLevel().size() == 0) {
                mHUDUtil.showErrorTip(getString(R.string.error_str));
                return;
            }
            getHiddenTypeOptionPickView(mPickViewVo).show();
        }
    }

    /**
     * 选择器Listener
     */
    private OptionsPickerView.OnOptionsSelectListener mOnOptionsSelectListener = new OptionsPickerView.OnOptionsSelectListener() {
        @Override
        public void onOptionsSelect(int position1, int position2, int position3) {
            if (mPickViewVo == null || mPickViewVo.getOneLevel() == null || mPickViewVo.getTwoLevel() == null || mPickViewVo.getOneLevel().isEmpty() || mPickViewVo.getTwoLevel().isEmpty()) {
                return;
            }
            mHiddenTypePosition1 = position1;
            mHiddenTypePosition2 = position2;
            hiddenTypeTv.setText(getHiddenCategoryText(mPickViewVo, position1, position2));
        }
    };

    /**
     * 组装隐患分类的显示值
     *
     * @param pickViewVo
     * @param position1
     * @param position2
     * @return
     */
    private String getHiddenCategoryText(HiddenTypePickViewVo pickViewVo, int position1, int position2) {
        return pickViewVo.getOneLevel().get(position1).getPickerViewText() + "-" + pickViewVo.getTwoLevel().get(position1).get(position2).getPickerViewText();
    }

    /**
     * 获取选择隐患分类的ID
     *
     * @return
     */
    private String getHiddenCategoryId() {
        return mPickViewVo.getTwoLevel().get(mHiddenTypePosition1).get(mHiddenTypePosition2).getTypeId() + "";
    }

    /**
     * 预计完成日期
     */
    private void pickFinishDate() {
        Calendar now = Calendar.getInstance();
        DatePickerDialog dpd = DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {

                Calendar calendar = Calendar.getInstance();
                calendar.set(year, monthOfYear, dayOfMonth);
                SimpleDateFormat dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                String dateStr = dateFormat.format(calendar.getTime());

                expectFinishDateTv.setText(CommonUtil.avoidNullString(dateStr));

            }
        }, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
        dpd.setThemeDark(false);
        dpd.vibrate(false);
        dpd.show(getFragmentManager(), Constants.DIALOG_TAG_CHECK_DATE);
    }

    /**
     * 检查日期
     */
    private void pickCheckDate() {
        Calendar now = Calendar.getInstance();
        DatePickerDialog dpd = DatePickerDialog.newInstance(new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {

                Calendar calendar = Calendar.getInstance();
                calendar.set(year, monthOfYear, dayOfMonth);
                SimpleDateFormat dateFormat = new SimpleDateFormat(TimeFormatConstants.FORMAT_DATE, Locale.getDefault());
                String dateStr = dateFormat.format(calendar.getTime());

                hiddenCheckTimeTv.setText(CommonUtil.avoidNullString(dateStr));

            }
        }, now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH));
        dpd.setThemeDark(false);
        dpd.vibrate(false);
        dpd.show(getFragmentManager(), Constants.DIALOG_TAG_CHECK_DATE);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(HttpCallbackEvent event) {
        if (EventTag.TAG_HIDDEN_SELF_CHECK_TYPE.equals(event.getTag())) {                            //隐患自查类型
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.showHUD();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();
                if (Constants.Result.SUCCESS == event.getEventResult()) {
                    List<SelfCheckType> result = (List<SelfCheckType>) event.getObj();
                    if (result != null && !result.isEmpty()) {
                        aSelfTypeName = new String[result.size()];
                        aSelfTypeId = new int[result.size()];
                        for (int i = 0; i < result.size(); i++) {
                            aSelfTypeName[i] = result.get(i).getTypeName();
                            aSelfTypeId[i] = result.get(i).getTypeId();
                        }
                        showSelfCheckTypeDialog();
                    }
                } else if (Constants.Result.ERROR == event.getEventResult()) {
                    mHUDUtil.showErrorTip(R.string.get_error);
                }
            }
        } else if (EventTag.TAG_HIDDEN_LEVEL.equals(event.getTag())) {                               //隐患级别
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.showHUD();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();
                if (Constants.Result.SUCCESS == event.getEventResult()) {
                    List<HiddenLevel> result = (List<HiddenLevel>) event.getObj();
                    if (result != null && !result.isEmpty()) {
                        aHiddenLevelName = new String[result.size()];
                        aHiddenLevelId = new int[result.size()];
                        for (int i = 0; i < result.size(); i++) {
                            aHiddenLevelName[i] = result.get(i).getLevelName();
                            aHiddenLevelId[i] = result.get(i).getLevelId();
                        }
                        showHiddenLevelDialog();
                    }
                } else if (Constants.Result.ERROR == event.getEventResult()) {
                    mHUDUtil.showErrorTip(R.string.get_error);
                }
            }
        } else if (EventTag.TAG_HIDDEN_TYPE.equals(event.getTag())) {                                //隐患分类
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.showHUD();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();
                if (Constants.Result.SUCCESS == event.getEventResult()) {
                    mHiddenTypes = (List<HiddenType>) event.getObj();
                    if (mHiddenTypes != null && !mHiddenTypes.isEmpty()) {
                        showHiddenCategoryPickView(mHiddenTypes);
                    }
                } else if (Constants.Result.ERROR == event.getEventResult()) {
                    mHUDUtil.showErrorTip(R.string.get_error);
                }
            }
        } else if (KeyConstants.URL_CREATE_SAFE_HIDDEN.equals(event.getTag())) {                     //创建新的隐患 有附件
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.showHUD();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();
                if (Constants.Result.SUCCESS == event.getEventResult()) {

                    mHUDUtil.showDoneTip("创建新隐患成功", new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {
                            callFinish();
                        }
                    });
                } else if (Constants.Result.ERROR == event.getEventResult()) {

                    mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {

                        }
                    });

                }
            }
        } else if (KeyConstants.URL_CREATE_SAFE_HIDDEN_NO_FILE.equals(event.getTag())) {             //创建新的隐患 无附件
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.showHUD();
            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                mHUDUtil.hidden();
                if (Constants.Result.SUCCESS == event.getEventResult()) {

                    mHUDUtil.showDoneTip("创建新隐患成功", new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {
                            callFinish();
                        }
                    });
                } else if (Constants.Result.ERROR == event.getEventResult()) {

                    mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {

                        }
                    });

                }
            }
        }

    }

    /**
     * 选择部门
     */
    private void showDepartmentList() {
        Bundle bundle = new Bundle();
        bundle.putString(KeyConstants.KEY_TAG, SafeHiddenCreateHiddenActivity.class.getName());
        UIHelper.showActivity(SafeHiddenCreateHiddenActivity.this, bundle, DepartmentListActivity.class);
    }

    /**
     * 选择人员
     */
    private void showPersonList() {
        Bundle bundle = new Bundle();
        bundle.putSerializable(KeyConstants.KEY_DEPARTMENT, departmentVo);
        bundle.putString(KeyConstants.KEY_TAG, SafeHiddenCreateHiddenActivity.class.getName());
        UIHelper.showActivity(SafeHiddenCreateHiddenActivity.this, bundle, PersonListActivity.class);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onDepartmentEvent(DepartmentEvent event) {

        if (SafeHiddenCreateHiddenActivity.class.getName().equals(event.getTag())) {
            departmentVo = event.getDepartmentVo();
            departmentTv.setText(CommonUtil.avoidNullString(departmentVo.getName()));
            responsibleTv.setText("");
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPersonEvent(PersonEvent event) {

        if (SafeHiddenCreateHiddenActivity.class.getName().equals(event.getTag())) {
            personVo = event.getPersonVo();
            responsibleTv.setText(CommonUtil.avoidNullString(personVo.getUser_name()));
        }
    }


    /**
     * 上传安全隐患信息
     */
    private void postSafeHiddenToCloud() {

        final LemonRequestParams params = new LemonRequestParams("");

        SafeHiddenVo safeHiddenVo = new SafeHiddenVo();

        safeHiddenVo.setReport_user_id(user.getUserId());
        safeHiddenVo.setCompany_id(user.getCompanyId());
        safeHiddenVo.setName(hiddenCheckPositionEt.getText().toString().trim());
        safeHiddenVo.setLocation("");

        safeHiddenVo.setSource_type("2");
        safeHiddenVo.setCheck_type(String.valueOf(aSelfTypeId[typeWhich]));
        safeHiddenVo.setClassify(getHiddenCategoryId());
        safeHiddenVo.setLevel(String.valueOf(aHiddenLevelId[levelWhich]));
        safeHiddenVo.setDesc(hiddenRemarkEt.getText().toString().trim());
        safeHiddenVo.setRectify_dept(departmentTv.getText().toString().trim());
        safeHiddenVo.setRectify_user_id(personVo.getUser_id());
        safeHiddenVo.setRectify_user_name(personVo.getUser_name());
        safeHiddenVo.setPlan_rectify_time(expectFinishDateTv.getText().toString().trim());
        safeHiddenVo.setPlan_rectify_money(expectMoneyEt.getText().toString().trim());

        params.put("data", mGson.toJson(safeHiddenVo));

        getUploadMediaItems(new SelectEvidenceAdapter.CropCallBack<Map<String, File>>() {
            @Override
            public void onStart() {
                mHUDUtil.show();
            }

            @Override
            public void onFinish(Map<String, File> data) {

                mHUDUtil.hidden();

                //Log.e("yxy", "data.isEmpty() = " + data.isEmpty());
                if(data.isEmpty()) {  //无附件
                    SafeHiddenServices
                            .init()
                            .createHiddenNoFile(params);
                } else {              //有附件
                    SafeHiddenServices
                            .init()
                            .createHidden(params, data);
                }
            }
        });

    }

}
